package com.jt.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jt.pojo.Cart;
import com.jt.pojo.User;
import com.jt.service.DubboCartService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/cart")
public class CartController {

    @Reference(check = false)
    private DubboCartService cartService;

    /**
     * 1.业务思路：当用户点击购物车按钮是，应该根据userId查询购物车嘻嘻你，之后再列表页面中展现。
     * 2.页面数据展现:利用￥{cartList}展现数据
     * @return
     */
    @RequestMapping("/show")
    public String show(Model model, HttpServletRequest request){

        User user = (User) request.getAttribute("JT_USER");
        //1.获取userId
        Long userId = user.getId();

        //1.获取userId  利用单点登录方式动态获取userId  暂时定死
//        Long userId = 7L;

        //2.根据userId查询购物车数据
        List<Cart> cartList = cartService.findCartListByUserId(userId);
        //利用model对象将数据添加到
        model.addAttribute("cartList",cartList);
        return "cart"; //跳转到购物车展现页面
    }


    /**
     * 业务需求：完成购物车更新操作
     * 1.url地址：cart/update/num
     * 2.请求参数：  563279-itemId
     * 3.返回值结果：void
     */
    @RequestMapping("/update/num/{itemId}/{num}")
    @ResponseBody
    private void updateCart(Cart cart, HttpServletRequest request){  //参数如果和属性名称一致，则可以直接赋值
        User user = (User) request.getAttribute("JT_USER");
        //1.获取userId
        Long userId = user.getId();

        //userId和itemId
//        Long userId = 7L;
        cart.setUserId(userId);
        cartService.updateCartNum(cart);
    }
    /**
     * url地址：http:/www/jt.com/cart/add/
     * 参数：表单数据提交 cart
     * 返回值：重定向到购物车展现页面
     */
    @RequestMapping("/add/{itemId}")
    public String saveCart(Cart cart, HttpServletRequest request){
        User user = (User) request.getAttribute("JT_USER");
        //1.获取userId
        Long userId = user.getId();

//        Long userId = 7L;
        cart.setUserId(userId);
        cartService.saveCart(cart);

        return "redirect:/cart/show.html";//维护伪静态策略
    }

    @RequestMapping("delete/{itemId}")
    public String deleteCart(@PathVariable Long itemId, HttpServletRequest request){
        User user = (User) request.getAttribute("JT_USER");
        //1.获取userId
        Long userId = user.getId();

//        Long userId = 7L;
        Cart cart = new Cart();
        cart.setUserId(userId);
        cart.setItemId(itemId);
        cartService.deleteCart(cart);
        return "redirect:";
    }
}
